using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using XFGameFramework;

namespace XFGameFramework.ReddotSystem
{
    /// <summary>
    /// 红点系统业务逻辑
    /// </summary>
    public class ReddotManager
    {

        #region 常量

        internal const string ON_REDDOT_PATH_VALUE_CHANGE = "ON_REDDOT_PATH_VALUE_CHANGE";

        #endregion


        #region 字段

        private static List<string> reddotPath = null;

        #endregion

        #region 属性

        /// <summary>
        /// 当前红点系统中所有的红点路径
        /// </summary>
        public static List<string> ReddotPath
        {
            get
            {
                if (reddotPath == null)
                    reddotPath = new List<string>();

                return reddotPath;
            } 
        }

        #endregion


        #region 事件

        /// <summary>
        /// 红点路径改变时触发
        /// </summary>
        public static Action onReddotPathChange;

        #endregion


        #region 方法

        //[RuntimeInitializeOnLoadMethod]
        //private static void InitUITipManager()
        //{
        //    reddotPath = null;
        //}


        /// <summary>
        /// 添加红点路径
        /// </summary>
        /// <param name="path"></param>
        public static void AddPath(string path)
        {

            if (string.IsNullOrEmpty(path)) {
                //Debug.Log("为空");
                return;
            }

            // 添加一个结尾符

            if (!path.EndsWith("/"))
                path = string.Format("{0}/", path); // 添加一个结尾符
              
            if (ReddotPath.Contains(path)) {
                //Debug.Log("已经有了");
                return; 
            }

            ReddotPath.Add(path);
            RefreshTipPath();
            onReddotPathChange?.Invoke(); 
        }



        /// <summary>
        /// 添加一组红点路径
        /// </summary>
        /// <param name="paths"></param>
        public static void AddPath(IList<string> paths)
        {
            if (paths == null || paths.Count == 0)
                return;

            foreach (string path in paths)
            {
                string p = path;

                if (!p.EndsWith("/"))
                    p = string.Format("{0}/", p); // 添加一个结尾符

                if (ReddotPath.Contains(p))
                    continue;
                ReddotPath.Add(p);
            }
             
            RefreshTipPath();
            onReddotPathChange?.Invoke();
        }


        /// <summary>
        /// 移除提示路径(内部使用,直接移除)
        /// </summary>
        /// <param name="path"></param>
        internal static void RemoveTipInternal(string path)
        {
            if (string.IsNullOrEmpty(path))
                return;

            if (!ReddotPath.Contains(path))
            {
                return;
            }

            ReddotPath.Remove(path);
            RefreshTipPath();
            onReddotPathChange?.Invoke();


            //Debug.LogFormat("移除红点路径:{0} 所有红点路径:{1}", path, JsonConvert.SerializeObject(ReddotPath));
        }

        /// <summary>
        /// 移除红点路径
        /// </summary>
        /// <param name="path"></param>
        public static void RemovePath(string path)
        {
            // 给路径添加一个结尾符(对应 AddTip)

            if (!path.EndsWith("/"))
                path = string.Format("{0}/", path); // 添加一个结尾符

            RemoveTipInternal(path);
        }

        /// <summary>
        /// 重置红点系统(清空所有提示)
        /// </summary>
        public static void Reset()
        {
            ReddotPath.Clear();
            RefreshTipPath();
        }

        /// <summary>
        /// 是否应该显示
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        internal static bool ShouldBeDisplay(string path)
        {

            if (string.IsNullOrEmpty(path))
                return false;

            if (ReddotPath.Count == 0)
                return false;

            foreach (var item in ReddotPath)
            {
                if (item.StartsWith(path))
                    return true;
            }


            return false;
        }

        private static void RefreshTipPath()
        { 
            // 触发事件 
            EventManager.TriggerEvent(ON_REDDOT_PATH_VALUE_CHANGE);
        }

        #endregion

    }
}

